package com.testplatfrom.project.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.testplatfrom.common.core.exception.project.ProjectException;
import com.testplatfrom.common.security.utils.SecurityUtils;
import com.testplatfrom.project.domain.TestplatfromDatabase;
import com.testplatfrom.project.domain.TestplatfromHttpconfiguration;
import com.testplatfrom.project.enumproject.database.DatabaseTypeEnum;
import com.testplatfrom.project.mapper.TestplatfromDatabaseMapper;
import com.testplatfrom.project.service.TestplatfromDatabaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.util.validation.feature.DatabaseType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 环境数据库链接配置 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2023-08-14
 */
@Service
@Slf4j
public class TestplatfromDatabaseServiceImpl extends ServiceImpl<TestplatfromDatabaseMapper, TestplatfromDatabase> implements TestplatfromDatabaseService {

    /**
     * 新增或者修改
     * @param testplatfromDatabaseList 数据库配置列表
     * @param environmentId 若是新增 则需要传环境id
     * @param bool 新增为true update 为false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void databaseCheck(List<TestplatfromDatabase> testplatfromDatabaseList, String environmentId, Boolean bool) {
        if (!CollectionUtils.isEmpty(testplatfromDatabaseList)){
            for (TestplatfromDatabase testplatfromDatabase : testplatfromDatabaseList) {
                DatabaseTypeEnum databaseType = DatabaseTypeEnum.valueOf(testplatfromDatabase.getDatabaseDriver());
                if (ObjectUtil.isNull(databaseType)){
                    throw new ProjectException("驱动类型错误");
                }
                testplatfromDatabase.setDatabaseDriver(databaseType.name());
            }
            //编辑
            if (!bool){
                LambdaQueryWrapper<TestplatfromDatabase> testplatfromHttpconfigurationLambdaQueryWrapper=new LambdaQueryWrapper<>();
                testplatfromHttpconfigurationLambdaQueryWrapper.eq(TestplatfromDatabase::getEnvironmentId, environmentId);
                this.remove(testplatfromHttpconfigurationLambdaQueryWrapper);
            }
            for (TestplatfromDatabase testplatfromDatabase : testplatfromDatabaseList) {
                testplatfromDatabase.setEnvironmentId(environmentId);
                testplatfromDatabase.setCreateBy(SecurityUtils.getUsername());
                testplatfromDatabase.setCreateTime(LocalDateTime.now());
            }
            this.saveBatch(testplatfromDatabaseList);
        }
    }

    @Override
    public Boolean databaseConnection(TestplatfromDatabase testplatfromDatabase) throws ClassNotFoundException {
        DatabaseTypeEnum databaseType = DatabaseTypeEnum.valueOf(testplatfromDatabase.getDatabaseDriver());
        if (ObjectUtil.isNull(databaseType)){
            throw new ProjectException("驱动类型错误");
        }
        Class.forName(databaseType.getDatabaseDriverType());//加载驱动类
        String url=testplatfromDatabase.getDatabaseConnectionUrl();
        String username=testplatfromDatabase.getDatabaseUsername();
        String password=testplatfromDatabase.getDatabasePassword();
        Connection conn= null;//用参数得到连接对象
        try {
            conn = DriverManager.getConnection(url,username,password);
            log.info("连接成功");
            log.info(conn.toString());
            return true;
        } catch (SQLException e) {
            throw new ProjectException(e.getMessage());
        }
    }

    @Override
    public List<TestplatfromDatabase> getDatabaseDetail(String environmentId) {
        List<TestplatfromDatabase> testplatfromDatabaseList = this.list(new LambdaQueryWrapper<TestplatfromDatabase>()
                .eq(TestplatfromDatabase::getEnvironmentId, environmentId));
        if (CollectionUtils.isEmpty(testplatfromDatabaseList)){
            return null;
        }
        return testplatfromDatabaseList;
    }
}
